package com.clever.mall.controller;

import com.clever.mall.commons.dto.TwoUserRequestParam;
import com.clever.mall.commons.dto.UserRequestParam;
import com.clever.mall.commons.dto.UserUpdateRequestParam;
import com.clever.mall.commons.exception.DatabaseException;
import com.clever.mall.commons.exception.ServiceException;
import com.clever.mall.commons.exception.UserException;
import com.clever.mall.commons.result.*;
import com.clever.mall.service.UserInfoService;
import com.clever.mall.utils.StatusCode;
import com.clever.mall.vo.UserVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Objects;

/**
 * @author ChenWang
 * @className PersonInfoConfroller
 * @date 2020/11/09 14:15
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/person")
public class UserInfoConfroller {
    @Resource
    UserInfoService userInfoService;

    /**
     * 根据传入的用户数据返回用户的个人信息首页
     *
     * @param userRequestParam 指定的用户信息
     * @return 返回指定的用户首页
     */
    @PostMapping("/my")
    public BaseResult<UserMyPage> home(@Validated @RequestBody UserRequestParam userRequestParam) {
        UserMyPage userMyPage = userInfoService.getUserMyPage(userRequestParam);
        if (Objects.isNull(userMyPage)) {
            throw new UserException(StatusCode.USER_NOT_EXIST_ERROR);
        }
        return BaseResult.success(userMyPage);
    }

    /**
     * 个人主页信息
     *
     * @param userRequestParam 指定的个人
     * @param page             页码
     * @param size             页面大小
     * @return 个人主页数据
     */
    @PostMapping("/myhome")
    public BaseResult<UserHomePage> entryHome(@Validated @RequestBody UserRequestParam userRequestParam,
                                              @RequestHeader(value = "page",required = false, defaultValue = "0") int page,
                                              @RequestHeader(value = "size",required = false, defaultValue = "6") int size) {
        UserHomePage userHomePage = userInfoService.getUserHomePage(userRequestParam, page, size);
        if (Objects.isNull(userHomePage)) {
            throw new UserException(StatusCode.USER_NOT_EXIST_ERROR);
        }
        return BaseResult.success(userHomePage);
    }

    /**
     * 他人信息主页
     *
     * @param twoUserRequestParam   两个数据，一个是浏览者的信息，一个是被浏览者的信息
     * @param page                  信息主页的页码
     * @param size                  信息主页的大小
     * @return 他人信息主页的信息
     */
    @PostMapping("otherHome")
    public BaseResult<OtherUserHomePage> entryOtherHome(@Validated @RequestBody TwoUserRequestParam twoUserRequestParam,
                                                   @RequestHeader(name = "page",required = false, defaultValue = "0") int page,
                                                   @RequestHeader(name = "size",required = false, defaultValue = "6") int size) {
        OtherUserHomePage otherUserHomePage = userInfoService.getOtherUserHomePage(twoUserRequestParam, page, size);
        if (Objects.isNull(otherUserHomePage)) {
            throw new UserException(StatusCode.USER_NOT_EXIST_ERROR);
        }
        return BaseResult.success(otherUserHomePage);
    }

    /**
     * 根据ID返回粉丝信息
     *
     * @param userRequestParam 指定的用户
     * @return 指定的粉丝信息
     */
    @PostMapping("/fans")
    public BaseResult<List<UserVo>> myFans(@Validated @RequestBody UserRequestParam userRequestParam) {
        List<UserVo> fansList = null;
        try {
            fansList = userInfoService.getFansList(userRequestParam);
            if (Objects.isNull(fansList)) {
                throw new UserException(StatusCode.USER_NOT_EXIST_ERROR);
            }
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        return BaseResult.success(fansList);
    }

    /**
     * 根据ID返回用户关注信息
     *
     * @param userRequestParam 指定的用户
     * @return 指定的关注信息
     */
    @PostMapping("/attentions")
    public BaseResult<List<UserVo>> myAttentions(@Validated @RequestBody UserRequestParam userRequestParam) {
        List<UserVo> fansList = null;
        try {
            fansList = userInfoService.getAttentionList(userRequestParam);
            if (Objects.isNull(fansList)) {
                throw new UserException(StatusCode.USER_NOT_EXIST_ERROR);
            }
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        return BaseResult.success(fansList);
    }

    /**
     * 用户的消息
     *
     * @param userRequestParam 指定的用户
     * @return 用户的消息数据
     */
    @PostMapping("/message")
    public BaseResult<MessagePage> myMessage(@Validated @RequestBody UserRequestParam userRequestParam) {
        MessagePage messagePage = userInfoService.getMessagePage(userRequestParam);
        if (Objects.isNull(messagePage)) {
            throw new UserException(StatusCode.USER_NOT_EXIST_ERROR);
        }
        return BaseResult.success(messagePage);
    }

    /**
     * 进入用户的编辑页面
     *
     * @param userRequestParam 指定的用户
     * @return 用户编辑前的信息
     */
    @PostMapping("/entryModify")
    public BaseResult<UserVo> entryModify(@Validated @RequestBody UserRequestParam userRequestParam) {
        UserVo userVo = userInfoService.getUserVo(userRequestParam);
        if (Objects.isNull(userVo)) {
            throw new UserException(StatusCode.USER_NOT_EXIST_ERROR);
        }
        return BaseResult.success(userVo);
    }
    /*@PostMapping("/wallet")
    public BaseResult<WalletPage> myWallet(@Validated @RequestBody UserRequestParam userRequestParam) {
        WalletPage walletPage = userInfoService.getMyWalletPage(userRequestParam);
        if(Objects.isNull(walletPage)){
            throw new UserException(StatusCode.USER_NOT_EXIST_ERROR);
        }
        return BaseResult.success(walletPage);
    }*/

    /**
     * 更改对他人的关注状态
     * @param twoUserRequestParam     关注者和被关注者的信息
     * @return                          1表示成功，0表示失败
     */
    @PostMapping("/changeAttention")
    public BaseResult<Integer> changeAttention(@Validated @RequestBody TwoUserRequestParam twoUserRequestParam
    ) {
        int flag = userInfoService.changeAttention(twoUserRequestParam);
        return BaseResult.success(flag);
    }

    /**
     *  更新用户信息
     * @param userUpdateRequestParam    指定的用户信息包
     * @return                          更新成功的信息包
     */
    @PostMapping("/updateDetail")
    public BaseResult<Integer> updateDetail(@Validated @RequestBody UserUpdateRequestParam userUpdateRequestParam){
        int flag = 0;
        try {
            flag = userInfoService.updateUserInfo(userUpdateRequestParam);
            if(flag == 0){
                throw new DatabaseException(StatusCode.DATABASE_UPDATE_USER_ERROR);
            }
        } catch (IOException e) {
            throw new ServiceException(StatusCode.SERVER_UPLOAD_ERROR);
        }
        return BaseResult.success(flag);
    }
}
